package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lst.api.entity.AssetsCreditInfo;
import com.lst.api.entity.AssetsHouseInfo;
import com.lst.api.mapper.AssetsHouseInfoMapper;
import com.lst.api.service.IAssetsHouseInfoService;
import com.lst.api.vo.AssetsHouseInfoVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 资产信息中的房产信息业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Service
public class AssetsHouseInfoService extends BaseService<AssetsHouseInfoMapper, AssetsHouseInfo> implements IAssetsHouseInfoService {

    @Override
    public boolean saveOrUpdate(long userId, String applyNo, AssetsHouseInfoVo assetsHouseInfoVo) {
        if (assetsHouseInfoVo == null) {
            return false;
        }
        assetsHouseInfoVo.check();
        AssetsHouseInfo assetsHouseInfo = new AssetsHouseInfo(userId, applyNo, assetsHouseInfoVo);
        QueryWrapper<AssetsHouseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(AssetsHouseInfo.APPLY_NO, applyNo)
                .eq(AssetsHouseInfo.PROMO_USER_ID, userId);
        int ret = super.saveNotExist(assetsHouseInfo, queryWrapper);
        if (ret == 0) {
            UpdateWrapper<AssetsHouseInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq(AssetsHouseInfo.APPLY_NO, applyNo)
                    .eq(AssetsHouseInfo.PROMO_USER_ID, userId)
                    .set(AssetsHouseInfo.UPDATE_AT, new Date())
                    .set(AssetsHouseInfo.ADDRESS_PROVINCE_CODE, assetsHouseInfo.getAddressProvinceCode())
                    .set(AssetsHouseInfo.ADDRESS_CITY_CODE, assetsHouseInfo.getAddressCityCode())
                    .set(AssetsHouseInfo.ADDRESS_AREA_CODE, assetsHouseInfo.getAddressAreaCode())
                    .set(AssetsHouseInfo.ADDRESS_DETAILS, assetsHouseInfo.getAddressDetails())
                    .set(AssetsHouseInfo.OWNER, assetsHouseInfo.getOwner())
                    .set(AssetsHouseInfo.BUY_TYPE, assetsHouseInfo.getBuyType())
                    .set(AssetsHouseInfo.TOTAL_LOAN_AMOUNT, assetsHouseInfo.getTotalLoanAmount())
                    .set(AssetsHouseInfo.MONTHLY_SUPPLY, assetsHouseInfo.getMonthlySupply())
                    .set(AssetsHouseInfo.LOAN_BANK_ID, assetsHouseInfo.getLoanBankId())
                    .set(AssetsHouseInfo.LOAN_YEARS, assetsHouseInfo.getLoanYears())
                    .set(AssetsHouseInfo.BUY_TOTAL_AMOUNT, assetsHouseInfo.getBuyTotalAmount())
                    .set(AssetsHouseInfo.BUILD_AREA, assetsHouseInfo.getBuildArea())
                    .set(AssetsHouseInfo.TOTAL_HOUSE, assetsHouseInfo.getTotalHouse());
            return super.update(updateWrapper);
        }
        return true;
    }

    @Override
    public AssetsHouseInfo getByApplyNo(String applyNo) {
        QueryWrapper<AssetsHouseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(AssetsHouseInfo.APPLY_NO, applyNo);
        return super.getOne(queryWrapper);
    }
}